import javax.swing.*;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ZhongJinTao
 * Date: 2024-03-12
 * Time: 16:05
 */
public class UdpEchoClient {
    DatagramSocket socket = null;
    String serverIp;
    int serverPort;

    public UdpEchoClient(String serverIp, int serverPort) throws SocketException {
        socket = new DatagramSocket();
        this.serverIp = serverIp;
        this.serverPort = serverPort;
    }

    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("->");
            //1、从控制台输入请求
            if(!scanner.hasNext()) {
                break;
            }
            String request = scanner.next();
            //2、把输入的请求构造成数据报,发送请求
            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),
                    request.getBytes().length, InetAddress.getByName(serverIp), serverPort);
            socket.send(requestPacket);
            //3、接受请求
            DatagramPacket responsePacket = new DatagramPacket(new byte[4096], 4096);
            socket.receive(responsePacket);
            //4、把请求转换成字符串，显示到控制台上
            String response = new String(responsePacket.getData(), 0, responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient udpEchoClient = new UdpEchoClient("127.0.0.1", 9090);
        udpEchoClient.start();
    }
}

//public class UdpEchoClient {
//    DatagramSocket socket = null;
//    String serverIP;
//    int serverPort;
//
//    public UdpEchoClient(String serverIP, int serverPort) throws SocketException {
//        socket = new DatagramSocket();
//        this.serverIP = serverIP;
//        this.serverPort = serverPort;
//    }
//
//    public void start() throws IOException{
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("客户端启动");
//        while (true) {
//            System.out.print("->");
//            //1、从控制台输入请求信息
//            if(!scanner.hasNext()) {
//                break;
//            }
//            String request = scanner.next();
//            //2、把请求信息构造成数据报，发送给服务器
//            DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),
//                    request.getBytes().length, InetAddress.getByName(serverIP), serverPort);
//            socket.send(requestPacket);
//            //3、接收服务器返回的响应
//            DatagramPacket responsePacket = new DatagramPacket(new byte[4096], 4096);
//            socket.receive(responsePacket);
//            //4、解析返回的响应，打印到控制台上
//            String response = new String(responsePacket.getData(), 0, responsePacket.getLength());
//            System.out.println(response);
//        }
//    }
//
//    public static void main(String[] args) throws IOException {
//        UdpEchoClient udpEchoClient = new UdpEchoClient("127.0.0.1", 9090);
//        udpEchoClient.start();
//    }
//}
